<template>
  <div class="login-wrap">
    <div class="login">
      <h4 class="login-text">登录</h4>
      <Form ref="form" :model="user" label-position="left" :rules="rules">
        <FormItem prop="username" :error="remoteMessage">
          <Input v-model="user.username">
            <Icon type="ios-contact" slot="prefix" />
          </Input>
        </FormItem>
        <FormItem prop="password" :error="remoteMessage">
          <Input type="password" v-model="user.password">
            <Icon type="md-lock" slot="prefix" />
          </Input>
        </FormItem>
        <!-- <FormItem>
          记住密码：
          <i-switch v-model="isSavePassword" />
        </FormItem>-->
        <FormItem>
          <Button type="primary" long @click="submit">登录</Button>
        </FormItem>
      </Form>
      <!-- <div>
        其他登录：
        <a href="javascript: ;">
          <Icon type="logo-github" />
        </a>
        <a href="javascript: ;">
          <Icon type="logo-QQ" />
        </a>
      </div>-->
      <div class="login-bottom">
        <router-link to="/register">注册</router-link>
        <router-link to="/findPwd">忘记密码</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      user: {
        user: "",
        password: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空"
          }
        ],
        password: [
          {
            required: true,
            message: "密码不能为空"
          }
        ]
      },
      remoteMessage: "",
      isSavePassword: false
    };
  },
  methods: {
    ...mapActions("account", ["login"]),
    submit() {
      this.$refs.form.validate(res => {
        if (!res) {
          return;
        }
        this.login(this.user)
          .then(() => {
            var redirectPath = this.$route.query.redirect;
            this.$router.push(redirectPath || "/");
          })
          .catch(() => {
            this.remoteMessage = "用户名或密码错误";
          });
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login-wrap {
  display: flex;
  justify-content: center;
  flex-direction: column;
  margin: auto;
  height: 100vh;
  width: 300px;
}
.login {
  border-radius: 5px;
  box-shadow: 0 0 5px #ccc;
  padding: 14px;
}
.login-text {
  text-align: center;
  padding-bottom: 12px;
}
.login-bottom {
  display: flex;
  justify-content: space-between;
}
</style>
